/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package respaldomysql;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Lolo
 */
public class Respaldo {

    public Respaldo() {
    }
       
    public Respaldo(int id, String directorio, String host, String hora, String usuario, String password, String bd) {
        this.id = id;
        this.directorio = directorio;
        this.host = host;
        this.hora = hora;
        this.usuario = usuario;
        this.password = password;
        this.bd = bd;
    }
 
    private int id;
    private String directorio;
    private String host;
    private String hora;
    private String usuario;
    private String password;
    private String bd;
    private Date diaUltimaEjecucion = new Date(2001,01,01);

    public Date getDiaUltimaEjecucion() {
        return diaUltimaEjecucion;
    }

    public void setDiaUltimaEjecucion(Date diaUltimaEjecucion) {
        this.diaUltimaEjecucion = diaUltimaEjecucion;
    }

    public String getBd() {
        return bd;
    }

    public void setBd(String bd) {
        this.bd = bd;
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDirectorio() {
        return directorio;
    }

    public void setDirectorio(String directorio) {
        this.directorio = directorio;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getHora() {
        return hora;
    }

    public void setHora(String hora) {
        this.hora = hora;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public static ArrayList<Respaldo> GetRespaldos(){

        ArrayList<Respaldo> respaldos = new ArrayList<Respaldo>();
        NodeList listaRespaldos = LeerXML();
        
        for (int i = 0; i < listaRespaldos.getLength(); i++) {

            Node respaldo = listaRespaldos.item(i);

            if (respaldo.getNodeType() == Node.ELEMENT_NODE) {
                Element elemento = (Element) respaldo;
                respaldos.add(new Respaldo(
                                    Integer.parseInt(GetTagValue("id", elemento)),
                                    GetTagValue("directorio", elemento),
                                    GetTagValue("host", elemento),
                                    GetTagValue("hora", elemento), 
                                    GetTagValue("u", elemento), 
                                    GetTagValue("p", elemento),
                                    GetTagValue("db", elemento)));
                System.out.println("" + GetTagValue("id", elemento) + "" + 
                                    GetTagValue("directorio", elemento) + "" + 
                                    GetTagValue("host", elemento) + "" + 
                                    GetTagValue("hora", elemento) + "" + 
                                    GetTagValue("u", elemento) + "" + 
                                    GetTagValue("p", elemento) + "" + 
                                    GetTagValue("db", elemento));
            }
        }
        
        return respaldos;
    }
    
    private static NodeList LeerXML() {
        DocumentBuilderFactory dbFactory;
        DocumentBuilder dBuilder;
        Document doc;
        NodeList listaRespaldos = null;        
        try {
            dbFactory = DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(new File("cnf.xml"));
            doc.getDocumentElement().normalize();
            listaRespaldos = doc.getElementsByTagName("respaldo");
        } catch (Exception e) {
            //Log.registrar(txt, "ERROR linea 296: " + e.getMessage() + "\r\n");
            javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
            e.printStackTrace();
        }
        return listaRespaldos;
    }

    private static String GetTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
}
